//
//  LoadAndSaveCoupons.m
//  Truffle
//
//  Created by m2sar on 13/03/10.
//  Copyright 2010 isrtream1-svn . All rights reserved.
//


/*
 - Récupérer la liste des descriptions de coupons :
 
 HTTP Method : GET /iphone/listcoupons
 
 Retour : JSON
 
 Exemple : Cf liste coupons
 Exemple : 
 {"couponsdescriptors":
 [
 {	
 "id":"1@fake",
 "name":"Mathématiques-Informatique",
 "position":"45-46",
 "provider":"bibliotheques-jussieu"
 },
 {	
 "id":"1@fake",
 "name":"Mathématiques-Informatique",
 "position":"45-46",
 "provider":"bibliotheques-jussieu"
 }
 ]
 }
 
 - Récupérer un coupon :
 
 HTTP Method : GET /iphone/coupon/${idCoupon}
 
 Query parameters : idClient	
 Retour : JSON
 
 Exemple : {"name":"Mathématiques-Informatique",
 "position":"45-46",
 "code":"8757560854676",
 "informations":
 [{"label":"ouverture","value":"Lundi au Vendredi 9h00-19h00","type":"DESC"},
 {"label":"telephone","value":"01 44 27 23 01","type":"DESC"},
 {"label":"email","value":"bibmie@upmc.fr","type":"DESC"},
 {"label":"places disponibles","value":"325/500 places","type":"QTE"}
 ]
 }
 
 
 
 */


#import "LoadAndSaveCoupons.h"
#import "LoadDetailsCoupon.h"
#import "RestConnection.h"
#import "DatasTruffle.h"
#import "Gateway.h"
#import "Coupon.h"
//#import "CouponTableViewController.h"

@implementation LoadAndSaveCoupons

@synthesize  myCaller, restMyConnection, gateway;






#pragma mark RestConnectionDelegate

- (void)willSendRequest:(NSURLRequest *)request
{
	NSLog(@"****************** [LoadAndSaveCoupons] [willSendRequest] CouponList : will send request : %@",request);
}


- (void)didReceiveResponse:(NSURLResponse *)response
{
	NSLog(@"****************** [LoadAndSaveCoupons] [didReceiveResponse]: %@", response);
}


- (void)finishedReceivingData:(NSData *)data callback:(NSString *)restconn
{
	
	NSLog(@"****************** [LoadAndSaveCoupons] [finishedReceivingData] CouponList :  %@", [restMyConnection stringData]);
	
	// Get the dictionary containing the raw parsed data.
	NSDictionary *rawCouponData = [restMyConnection dictionaryData];  // REAL VERSION
	
	mutableCouponsTable = [[NSMutableArray alloc] init];
	
	UIDevice * device = [UIDevice currentDevice];
	
	// Get the interesting search results data for display to the user.
	// 
	couponsTableData = [[rawCouponData objectForKey:@"coupondescriptors"] retain];
	if ([couponsTableData count] == 0) {
		NSLog(@"****************** [LoadAndSaveCoupons] [finishedReceivingData] Pas de coupons disponibles sur la passerelle");
	}else {
		for(int cpt=0;cpt<[couponsTableData count];cpt++) {
			
			//Service *tempserv = [[Service alloc] initService:[[servicesTableData objectAtIndex:cpt] objectForKey:@"id"] withPosition:[[servicesTableData objectAtIndex:cpt] objectForKey:@"position"] withName:[[servicesTableData objectAtIndex:cpt] objectForKey:@"name"] withProvider:[[servicesTableData objectAtIndex:cpt] objectForKey:@"provider"]];
			//[mutableServicesTable addObject:tempserv];
			
			NSDictionary * dic = [couponsTableData objectAtIndex:cpt];
			//Coupon *tempCoupon = [[Coupon alloc] initWithName:[dic objectForKey:@"name"] description:[dic objectForKey:@"description"] provider:[dic objectForKey:@"provider"] andData:[dic objectForKey:@""]];
			if(![DatasTruffle isConnected]) {
				NSLog(@"application not connected, cannot check for new coupons");
				//exit(1);
			} else {
				// etablir une connexion pour récupérer les nouveaux coupons sur la passerelle 
				restMyConnection = [RestConnection new];
				// si le coupon est vide ou "Not Found"
				if ( ([[dic objectForKey:@"name"]isEqualToString:@"Not found"]) ||  
					([[dic objectForKey:@"name"]isEqualToString:@""])
					){
					continue;
				}
				restMyConnection.baseURLString = [NSString stringWithFormat:@"%@/iphone/coupon/%@?idClient=%@", 
												  gateway,
												  [dic objectForKey:@"id"],
												  [device uniqueIdentifier]
												  ];
				LoadDetailsCoupon *loadDetailsCoupon    = [[LoadDetailsCoupon alloc] init];
				loadDetailsCoupon.idCurrentCoupon       = [dic objectForKey:@"id"];
				loadDetailsCoupon.providerCurrentCoupon = [dic objectForKey:@"provider"];
				loadDetailsCoupon.myRestConnection      = restMyConnection;
				loadDetailsCoupon.myCaller = self.myCaller;
				restMyConnection.delegate                 = loadDetailsCoupon;
				
				
				
				NSString *urlString = @"";
				[restMyConnection performRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];				
			}
		}		
	}
}


@end
